package dashboardSwing;

import top.anka1.ims.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Objects;

public class DashboardGUI {
    private OSinfo osInfo = new OSinfo();
    public Taskbar taskbar = Taskbar.getTaskbar();
    public ImageIcon icon = new ImageIcon(Objects.requireNonNull(DashboardGUI.class.getResource("Java_1319x1319.png")));
    public void windowCenterAlignment(JFrame frame) {
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int windowx = (int) screensize.getWidth() / 2 - frame.getWidth() / 2;
        int windowy = (int) screensize.getHeight() / 2 - frame.getHeight() / 2;
        frame.setLocation(windowx, windowy);
    }
    public void MainGUI() {
        JFrame mainFrame = new JFrame("Dashboard");
        mainFrame.setIconImage(icon.getImage());
        if (osInfo.isMacOS() | osInfo.isMacOSX()) {
            taskbar.setIconImage(icon.getImage());
        }
        mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        mainFrame.addWindowListener(new WindowAdapter() {
            /**
             * Invoked when a window is in the process of being closed.
             * The close operation can be overridden at this point.
             *
             * @param e
             */
            @Override
            public void windowClosing(WindowEvent e) {
                //super.windowClosing(e);
                int result = JOptionPane.showConfirmDialog(mainFrame, "Are you sure you want to exit?", "Exit Confirmation", JOptionPane.YES_NO_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            }
        });
        mainFrame.setSize(400, 300);
        windowCenterAlignment(mainFrame);
        JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 10, 10)); // 按钮的网格布局面板

        JButton zooButton = new JButton("Open Zoo Management System");
        zooButton.addActionListener(e -> {
            ZooGUI zooGUI = new ZooGUI();
            zooGUI.MainGUI();
        });
        buttonPanel.add(zooButton);

        JButton bankButton = new JButton("Open Banking Task Management System");
        bankButton.addActionListener(e -> {
            BankGUI bankGUI = new BankGUI();
        });
        buttonPanel.add(bankButton);

        JButton restaurantButton = new JButton("Open Restaurant Management System");
        restaurantButton.addActionListener(e -> {
            RestaurantGUI restaurantGUI = new RestaurantGUI();
            restaurantGUI.MainGUI();
        });
        buttonPanel.add(restaurantButton);

        JButton shapeButton = new JButton("Open ShapeGUI");
        shapeButton.addActionListener(e -> {
            ShapeGUI shapeGUI = new ShapeGUI();
            shapeGUI.MainGUI();
        });
        buttonPanel.add(shapeButton);

        JButton AboutButton = new JButton("About");
        AboutButton.addActionListener(e -> {SwingUtilities.invokeLater(About_Copyright::new);});
        buttonPanel.add(AboutButton);

        JButton exitButton = new JButton("Exit");
        exitButton.setForeground(Color.RED);
        exitButton.addActionListener(e -> {
            int result = JOptionPane.showConfirmDialog(mainFrame, "Are you sure you want to exit?", "Exit Confirmation", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        });
        buttonPanel.add(exitButton);

        //mainFrame.setUndecorated(true);

        // 包装一个带边距的外层容器
        JPanel wrapper = new JPanel(new BorderLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // 设置边距
        wrapper.add(buttonPanel, BorderLayout.CENTER);

        mainFrame.add(wrapper);

        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            DashboardGUI dashboardGUI = new DashboardGUI();
            dashboardGUI.MainGUI();
        });
    }
}
